//
//  ViewController.m
//  小球移动
//
//  Created by Justsoso on 16/4/7.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

#import <CoreMotion/CoreMotion.h>

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *ballImageView;

@property (nonatomic,strong) CMMotionManager *motionManager;

@property (nonatomic,assign) BOOL *playing;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.motionManager = [[CMMotionManager alloc] init];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if (self.motionManager.accelerometerAvailable)
    {
        
        self.motionManager.accelerometerUpdateInterval = 0.001;
        [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
            CGFloat x = self.ballImageView.frame.origin.x + accelerometerData.acceleration.x;
            CGFloat y = self.ballImageView.frame.origin.y - accelerometerData.acceleration.y;
            if ((x < 355 && x > 0) && (y < 647 && y > 0))
            {
                self.ballImageView.frame = CGRectMake(x , y , 20, 20);
            }
            
//            self.motionManager.accelerometerUpdateInterval = accelerometerData.acceleration.z / 1000000.0f;
            
        }];
    }
    
}


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [self.motionManager stopAccelerometerUpdates];
}
@end
